﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Assignment_2
{
    public class Camera
    {
        public Matrix View { get; set; }
        public Matrix Projection { get; set; }
        public Matrix Rotation { get; set; }

        public Vector3 Position { get; set; }
        private Vector3 _cameraTarget;
        private Vector3 _cameraUpVector = Vector3.Up;
        private Vector3 _campos;
        private Vector3 _camup;

        public Camera(Vector3 position)
        {
            Position = position;
            Rotation = Matrix.Identity;
        }

        public void InitializeCamera(Vector3 cameraTarget, float aspectRatio)
        {
            _cameraTarget = cameraTarget;
            Position = cameraTarget;

            _campos = Position;
            _camup = Vector3.Up;

            Projection = Matrix.CreatePerspectiveFieldOfView(
                MathHelper.PiOver4,
                aspectRatio,
                0.1f,
                1000.0f);

            View = Matrix.CreateLookAt(
                Position,
                _cameraTarget,
                _cameraUpVector);
        }

        public void Update()
        {
            _campos = new Vector3(0f, 20f, -20f);
            _campos = Vector3.Transform(_campos, Rotation);
            _campos += Position;

            _camup = new Vector3(0, 1, 0);
            _camup = Vector3.Transform(_camup, Rotation);

            View = Matrix.CreateLookAt(
                    _campos,
                    Position,
                    _camup);
        }

        public void Draw(BasicEffect effect)
        {
            effect.View = View;
            effect.CurrentTechnique.Passes[0].Apply();
        }
    }
}
